<?php
/**
 * Created by PhpStorm.
 * User: George
 * Date: 2017/9/25
 * Time: 11:06
 */

namespace AdminBundle\Task;


class TimeLineB2CTask extends Task
{
    public function jsonTask()
    {
        $data = [
            'app' => 'api', 'os' => 'ios', 'deviceid' => $this->task->getDeviceName(),
            'data' =>
                [
                    'token' => $this->task->getAction(),
                    'webchat' => [$this->task->getWebchatUsername(), $this->task->getNote()]
                ]
        ];

        $url = 'http://admin.bjtwtx.com/api/partner/moment/b2c';

        $json = $this->httpJson($url, $data);
        if ($json == null || $json['code'] != 0) {
            return null;
        }
        $data = $json['data'];

        return json_encode([
            'content' => $data['content'],
            'images' => $this->getImages($data['images'], $data['domain']),
            'comment' => '',
            'like' => (bool)(mt_rand(1, 10) % 2),
            'type' => $this->getType($data['content'], $data['images'])
        ]);
    }

    private function getImages(&$images, $domain)
    {
        if (!is_array($images)) {
            return [];
        }
        $result = [];
        foreach ($images as $image) {
            $result[] = $domain . $image;
        }
        return $result;
    }

    private function getType(&$content, &$images)
    {
        if (!empty($content) && !empty($images)) {
            return 'mixed';
        } elseif (empty($content) && !empty($images)) {
            return 'image';
        } elseif (!empty($content) && empty($images)) {
            return 'text';
        }
        return 'none';
    }

}